package com.xuecheng.content.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.model.dto.SaveTeachplanDto;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.content.service.TeachplanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description 课程计划编辑接口
 * @author Mr.M
 * @date 2022/9/6 11:29
 * @version 1.0
 */
@Api(value = "课程计划编辑接口",tags = "课程计划编辑接口")
@RestController
public class TeachplanController {

    @Autowired
    TeachplanService teachplanService;
    @Autowired
    TeachplanMapper teachplanMapper;
    @Autowired
    CourseTeacherMapper courseTeacherMapper;

    @ApiOperation("查询课程计划树形结构")
    @ApiImplicitParam(value = "courseId",name = "课程基础Id值",required = true,dataType = "Long",paramType = "path")
    @GetMapping("/teachplan/{courseId}/tree-nodes")
    public List<TeachplanDto> getTreeNodes(@PathVariable Long courseId){
        return teachplanService.findTeachplanTree(courseId);
    }

    @ApiOperation("课程计划创建或修改")
    @PostMapping("/teachplan")
    public void saveTeachplan( @RequestBody SaveTeachplanDto teachplan){
        teachplanService.saveTeachplan(teachplan);
    }
    @ApiOperation("课程计划删除")
    @DeleteMapping("/teachplan/{id}")
    public void delete (@PathVariable Long id){
        Teachplan teachplan1 = teachplanService.getById(id);

        Teachplan teachplan = teachplanMapper.selectOne(new LambdaQueryWrapper<Teachplan>().eq(Teachplan::getParentid, id));
        if (teachplan != null ){
            XueChengPlusException.cast("120409","请选择要删除的子计划");
        }
        teachplanService.removeById(id);

        Long parentId = teachplan1.getParentid();
        // 3. 查询所有同级节点并按 orderby 升序排列
        LambdaQueryWrapper<Teachplan> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Teachplan::getParentid, parentId)
                .orderByAsc(Teachplan::getOrderby);

        List<Teachplan> siblings = teachplanMapper.selectList(wrapper);

        // 4. 重新设置 orderby
        for (int i = 0; i < siblings.size(); i++) {
            Teachplan plan = siblings.get(i);
            plan.setOrderby(i + 1);
            teachplanService.updateById(plan);// 从 1 开始重新排序
        }


    }
    @ApiOperation("课程计划排序")
    @PostMapping("/teachplan/{moveType}/{id}")
    public void order(@PathVariable String moveType,@PathVariable Long id){
        teachplanService.order(moveType,id);
    }

    @ApiOperation("课程老师查询")
    @GetMapping("/courseTeacher/list/{courseId}")
    public List<CourseTeacher> getTeacher(@PathVariable Long courseId) {
        return courseTeacherMapper.selectList(new QueryWrapper<CourseTeacher>().eq("course_id", courseId));
    }
    @ApiOperation("添加教师")
    @PostMapping("/courseTeacher")
    public void addTeacher (@RequestBody CourseTeacher courseTeacher) {

        if (courseTeacher.getTeacherName() == null || courseTeacher.getCourseId() == null){
            XueChengPlusException.cast("120410","请填写教师名称和课程Id");
        }
        courseTeacherMapper.insert(courseTeacher);

    }
    @ApiOperation("删除教师")
    @DeleteMapping("/courseTeacher/course/{courseId}/{teacherId}")
    public void deleteTeacher (@PathVariable Long courseId,@PathVariable Long teacherId) {
        courseTeacherMapper.delete(new LambdaQueryWrapper<CourseTeacher>().eq(CourseTeacher::getCourseId, courseId).eq(CourseTeacher::getId, teacherId));
    }


}